<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/09/12
 * Time: 11:31
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

echo date('Y-m-d H:i:s',time())." service statistics cancel started \n";

//查询C端所有的订单
$orders = Orders::whereIn('order_source',[2,10])->select('order_id')->get();
if(!is_null($orders)){
    foreach($orders as $key => $val){
        $res = OrderProduct::where('order_id',$val->order_id)->where('status',1)->whereIn('product_type',['base','activity'])->get();
        if(!is_null($res)){
            foreach($res as $k =>$v){
                $service_type = 1;
                if($v->product_type == 'base'){
                    $service_type = 2;
                }
                if($v->product_type == 'activity'){
                    $service_type = 1;
                }
                if($service_type != 1){
                    $service = AhpServiceStatistics::where('service_type',$service_type)->where('product_id',$v->product_id)->first();
                    if(!is_null($service)){
                        AhpServiceStatistics::where('service_type',$service_type)->where('product_id',$v->product_id)->update(array('num' => $service->num + 1, 'updated_at' => Common::currentTimeString()));
                    }else{
                        $product = new AhpServiceStatistics();
                        $product->service_type = $service_type;
                        $product->product_id = $v->product_id;
                        $product->num = 1;
                        $product->created_at = Common::currentTimeString();
                        $product->updated_at = Common::currentTimeString();
                        $product->save();
                    }
                }
            }
        }
    }

    //查询ahp服务
    $ahp_service = AhpService::get();
    if(!is_null($ahp_service)){
        foreach($ahp_service as $key => $val) {
            if ($val->service_type == 1) {
                $ahp_service[$key]->service = AhpServiceMktCms::join('product_activity', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_activity.id')->where('ahp_service_mkt_cms.service_id', $val->id)->select('product_activity.id', 'product_activity.name', 'price', 'product_activity.type', 'discount')->get()->toArray();
            } else {
                $ahp_service[$key]->service = AhpServiceMktCms::join('product_package', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_package.id')->where('ahp_service_mkt_cms.service_id', $val->id)->select('product_package.id', 'product_package.name')->get()->toArray();
            }
            $product_id = array_column($ahp_service[$key]->service,'id');
            $count = AhpServiceStatistics::whereIn('product_id',$product_id)->where('service_type',$val->service_type)->sum('num');
            AhpService::where('id',$val->id)->increment('num',$count);
        }
    }
}

echo date('Y-m-d H:i:s',time())." service statistics cancel ended \n";